<template>
  <div class="add-form">
    <el-dialog :title="dialogTitle" :visible.sync="dialogFormVisible" width="40%" @close="closeHandle()" >
      <el-form ref="dataForm" :model="formBase" label-position="left" label-width="150px">
        <el-form-item label="服务名称：" prop="name">
          <el-input v-model="formBase.name" style="width: 194px" placeholder="请输入名称" ></el-input>
        </el-form-item>
        <el-form-item label="简要描述：" prop="introduce" >
          <el-input type="textarea" :rows="2" style="max-width: 200px;" v-model="formBase.info" placeholder="请输入简要描述"></el-input>
        </el-form-item>
        <el-form-item label="服务分类：" prop="type2">
          <el-select v-model="formBase.serviceSubTypeId" placeholder="请选择">
            <el-option v-for="item in typeList" :key="item.id" :value="item.id" :label="item.name" ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="服务价格：" prop="normalPrice" >
          <el-input v-model="formBase.normalPrice" style="width: 194px" placeholder="请输入服务价格"></el-input>
        </el-form-item>
        <el-form-item label="选择图片：" prop="normalPrice">
          <el-upload
            action="http://68.79.1.140:8272/upload"
            :data="fullUpType"
            :file-list="fileList"
            :on-success="uploadSuccess"
            :on-remove="handleRemove"
            list-type="picture-card"
          >
            <i class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="详情描述：" prop="introduce" >
          <el-input type="textarea" :rows="3" style="max-width: 500px;" v-model="formBase.details" placeholder="请输入内容"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormH">取消</el-button>
        <el-button type="primary" @click="createData">确认</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { updateData, addData } from '@/api/service'
export default {
  name: 'add',
  props: ['dialogTitle', 'typeList'],
  data() {
    return {
      dialogFormVisible: false,
      formBase: {
        name: '',
        info: '',
        normalPrice: '',
        isbeginPrice: '',
        imgUrl: '',
        serviceSubTypeId: '',
        storeId: this.$store.getters.storeId,
        details: ''
      },
      fullUpType: { type: 7, extension: 'img' },
      fileList: []
    }
  },
  computed: {},
  methods: {
    // 弹层显示
    dialogFormV(val) {
      this.dialogFormVisible = true
      if (val !== undefined) {
        this.formBase = val
        this.fileList = val.imgUrl ? [{name: 'img', url: val.imgUrl}] : []
      }
    },
    // 弹层隐藏
    dialogFormH() {
      this.dialogFormVisible = false
    },
    // 图片上传成功
    uploadSuccess(obj, file) {
      this.formBase.imgUrl = obj
      // updatedImg(file, obj, this.typeTip)
    },
    //
    handleRemove(file, fileList) {
      this.uploadDisabled = false
    },
    closeHandle() {
      this.formBase = {}
      this.fileList = []
    },
    // 表单提交
    createData() {
      this.$refs['dataForm'].validate(async valid => {
        if (valid) {
          this.dialogFormH()
          let data = {
            ...this.formBase
          }
          if (this.formBase.id) {
            await updateData(data).then((res) => {
              this.$emit('newDataes', this.formBase)
            })
          } else {
            await addData(this.formBase).then((res) => {
              this.$emit('newDataes', this.formBase)
            })
          }
        } else {
          this.$message.error('*号为必填项!')
        }
      })
    }
  },
  // 挂载结束

  mounted: function() {},
  // 创建完毕状态
  created() {
  },
  // 组件更新
  updated: function() {}
}
</script>
<style>
.el-form--label-left .el-form-item__label {
  text-align: right;
}
.el-form-item--medium {
  margin-bottom: 22px;
}
.el-dialog__footer {
  text-align: center;
}
</style>
